package com.badboy.mapper;

import com.badboy.entity.Posts;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author 26607
* @description 针对表【posts(存储用户发布的主题帖及元数据)】的数据库操作Mapper
* @createDate 2025-05-10 21:16:11
* @Entity com.badboy.entity.Posts
*/
public interface PostsMapper {

    @Select("<script>" +
            "SELECT p.*, u.username " +
            "FROM posts p " +
            "JOIN users u ON p.user_id = u.user_id " +
            "WHERE p.is_deleted = 0 " +
            "<if test='forumId != null'> AND p.forum_id = #{forumId}</if> " +
            "ORDER BY " +
            "<choose>" +
            "  <when test='sortType == \"hot\"'>p.views DESC</when>" +
            "  <otherwise>p.created_at DESC</otherwise>" +
            "</choose> " +
            "LIMIT #{pageSize} OFFSET #{offset}" +
            "</script>")
    @Results({
            @Result(property = "postId", column = "post_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "forumId", column = "forum_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "views", column = "views"),
            @Result(property = "isPinned", column = "is_pinned"),
            @Result(property = "username", column = "username")
    })
    List<Posts> selectPosts(@Param("forumId") Integer forumId,
                            @Param("sortType") String sortType,
                            @Param("pageSize") int pageSize,
                            @Param("offset") int offset);

    @Select("SELECT COUNT(*) FROM posts WHERE is_deleted = 0 AND forum_id = #{forumId}")
    int countByForum(int forumId);



    @Select("<script>" +
            "SELECT p.*, u.username " +
            "FROM posts p " +
            "JOIN users u ON p.user_id = u.user_id " +
            "WHERE p.is_deleted = 0 " +
            "<if test='forumId != null'> AND p.forum_id = #{forumId}</if> " +
            "<if test='keyword != null'> AND (p.content LIKE CONCAT('%', #{keyword}, '%') OR p.title LIKE CONCAT('%', #{keyword}, '%'))</if> " +
            "ORDER BY " +
            "<choose>" +
            "  <when test='sortType == \"hot\"'>p.views DESC</when>" +
            "  <otherwise>p.created_at DESC</otherwise>" +
            "</choose>" +
            "</script>")
    @Results({
            @Result(property = "postId", column = "post_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "forumId", column = "forum_id"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "views", column = "views"),
            @Result(property = "isPinned", column = "is_pinned"),
            @Result(property = "username", column = "username")
    })
    List<Posts> searchPostsByContent(@Param("forumId") Integer forumId,
                                    @Param("keyword") String keyword,
                                    @Param("sortType") String sortType);





    int deleteByPrimaryKey(Long id);

    int insert(Posts record);

    int insertSelective(Posts record);

    Posts selectByPrimaryKey(Long id);

    @Update("UPDATE posts SET is_deleted = 1 WHERE post_id = #{postId}")
    int logicalDeleteByPrimaryKey(@Param("postId") Long postId);

    int updateByPrimaryKeySelective(Posts record);

    int updateByPrimaryKey(Posts record);



    // 带参数的查询
    @Select("SELECT " +
            "    ps.post_id, " +
            "    ps.title, " +
            "    pm.CommentCount, " +
            "    pm.likeCount " +
            "FROM " +
            "    posts ps " +
            "JOIN " +
            "    post_management pm ON ps.post_id = pm.postCount_id " +
            "WHERE ps.post_id = #{postId}")
    @Results({
            @Result(property = "postId", column = "post_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "management.postCountId", column = "postCount_id"),
            @Result(property = "management.commentCount", column = "CommentCount"),
            @Result(property = "management.likeCount", column = "likeCount")
    })
    Posts selectPostWithManagementInfo(@Param("postId") Long postId);
}
